<!--
  - Copyright 2011 Angel Sanadinov
  -
  - This file is part of VBox WMI.
  -
  - VBox WMI is free software: you can redistribute it and/or modify
  - it under the terms of the GNU General Public License as published by
  - the Free Software Foundation, either version 3 of the License, or
  - (at your option) any later version.
  -
  - VBox WMI is distributed in the hope that it will be useful,
  - but WITHOUT ANY WARRANTY; without even the implied warranty of
  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  - GNU General Public License for more details.
  -
  - You should have received a copy of the GNU General Public License
  - along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
  -->

<%@tag import="Beans.Responses.DataRetrievalComponents.UserDataComponent"%>
<%@tag import="Beans.Responses.DataRetrievalResponse"%>
<%@tag import="Utilities.Constants.ResourceRequestType"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstlc" %>
<%@tag description="Prints user(s) data." pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>

<%-- Attributes --%>
<%@attribute name="data" required="true" description="User(s) data retrieval object."
             rtexprvalue="true" type="DataRetrievalResponse<UserDataComponent>"%>

<%@attribute name="userId" required="true"
             description="Current user ID. Used for setting up the UI properly."
             rtexprvalue="true" type="java.lang.Integer"%>

<%@attribute name="isManager" required="true" description="Used for setting up the UI properly."
             rtexprvalue="true" type="java.lang.Boolean"%>
<%-- End of Attributes --%>

<%-- Page Body Content --%>
<jstlc:choose>
    <jstlc:when test="${data == null}">
        Failed to retrieve user(s) data object.
    </jstlc:when>

    <jstlc:when test="${data.requestResult && data.valid}">
        <jstlc:choose>
            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.SINGLE_RESOURCE %>">
                <table id="mainUserTable" class="mainResourceDataTable">
                    <tr><td>User Id:</td><td>${data.resource.userData.userId}</td></tr>
                    <tr><td>User Name:</td><td>${data.resource.userData.username}</td></tr>
                    <tr><td>Creation Date:</td><td>${data.resource.userData.dateCreated}</td></tr>
                    <tr><td>Last Login Date:</td><td>${data.resource.userData.dateLastLogin}</td></tr>
                    <tr><td>Last Login IP:</td><td>${data.resource.userData.lastLoginAddress}</td></tr>
                    <tr><td>Account Level:</td><td>${data.resource.accountLevel}</td></tr>
                    <tr><td>Logged In:</td><td>${data.resource.userLoggedIn ? "Yes" : "No"}</td></tr>
                    <tr><td>Permissions:</td><td><a class="actionLink" href="noJavaScript.jsp"
                                                    onclick="return changePopupVisibility('userPermissionsTable');">View</a></td></tr>
                    <tr><td>User Logs:</td><td><a class="actionLink" href="logs.jsp?limit=initiator&amp;initiatorId=${data.resource.userData.userId}">View</a></td></tr>
                </table>
                <div id="userOperations">
                    <jstlc:if test="${isManager}">
                    <a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formModifyUser');">Modify User</a><br>
                    </jstlc:if>
                    <a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formResetUserPassword');">Reset Password</a><br>
                </div>
                <form class="resourceModification" id="formModifyUser" action="manage_user.jsp?action=2&amp;id=${data.resource.userData.userId}" method="post">
                    <table>
                        <tr><td>Account Level:</td><td><select name="user.modify.level">
                                    <option value="0" ${data.resource.userData.accountLevel == 0 ? "selected" : ""}>Disabled</option>
                                    <option value="1" ${data.resource.userData.accountLevel == 1 ? "selected" : ""}>Normal</option>
                                    <option value="2" ${data.resource.userData.accountLevel == 2 ? "selected" : ""}>Manager</option>
                                    <option value="3" ${data.resource.userData.accountLevel == 3 ? "selected" : ""}>Owner</option>
                                </select></td></tr>
                        <tr><td><input type="submit" value="Update"></td></tr>
                    </table>
                </form>
                <form class="resourceModification" id="formResetUserPassword" action="manage_user.jsp?action=4&amp;id=${data.resource.userData.userId}" method="post">
                    <table>
                        <jstlc:if test="${userId == data.resource.userData.userId}">
                        <tr><td>Old Password:</td><td><input type="password" name="reset.oldPassword"></td></tr>
                        </jstlc:if>
                        <tr><td>New Password:</td><td><input type="password" name="reset.newPassword"></td></tr>
                        <tr><td>Confirm New Password:</td><td><input type="password" name="reset.newPassword.confirm"></td></tr>
                        <tr><td><input type="submit" value="Reset"></td></tr>
                    </table>
                </form>
                <table class="resourceModification" id="userPermissionsTable">
                    <tr><td>Server ID</td><td>Machines</td><td>Media</td><td>Networks</td></tr>
                <jstlc:forEach var="permissions" items="${data.resource.permissionsData}">
                    <tr><td>${permissions.serverId}</td>
                    <td>${permissions.machinesPermissions}</td>
                    <td>${permissions.mediaPermissions}</td>
                    <td>${permissions.networksPermissions}</td>
                    <jstlc:if test="${isManager}">
                    <td><a class="actionLink" href="manage_user.jsp?action=3&amp;permissions&amp;id=${data.resource.userData.userId}&amp;serverId=${permissions.serverId}">Remove</a></td>
                    <td><a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formModifyPermissions${permissions.serverId}', true);">Modify</a></td>
                    <td><form class="resourceModification" id="formModifyPermissions${permissions.serverId}" method="post"
                              action="manage_user.jsp?action=2&amp;permissions&amp;id=${data.resource.userData.userId}&amp;serverId=${permissions.serverId}">
                            <table>
                                <tr><td>Modify Permissions for Server ${permissions.serverId}:</td></tr>
                                <tr><td>Machines:</td><td><input type="text" size="2" name="permissions.modify.machines" value="${permissions.machinesPermissions}"></td></tr>
                                <tr><td>Media:</td><td><input type="text" size="2" name="permissions.modify.media" value="${permissions.mediaPermissions}"></td></tr>
                                <tr><td>Networks:</td><td><input type="text" size="2" name="permissions.modify.networks" value="${permissions.networksPermissions}"></td></tr>
                                <tr><td><input type="submit" value="Update"></td></tr>
                            </table>
                        </form></td>
                    </jstlc:if></tr>
                </jstlc:forEach>
                    <jstlc:if test="${isManager}">
                    <tr><td><a class="actionLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formCreatePermissions', true);">Add Permissions</a></td>
                        <td><form class="resourceCreationElement" id="formCreatePermissions"
                                  action="manage_user.jsp?action=1&amp;permissions&amp;id=${data.resource.userData.userId}" method="post">
                                <table>
                                    <tr><td>Add Permissions:</td></tr>
                                    <tr><td>Server Id:</td><td><input type="text" size="3" name="permissions.create.server"></td></tr>
                                    <tr><td>Machines:</td><td><input type="text" size="2" name="permissions.create.machines"></td></tr>
                                    <tr><td>Media:</td><td><input type="text" size="2" name="permissions.create.media"></td></tr>
                                    <tr><td>Networks:</td><td><input type="text" size="2" name="permissions.create.networks"></td></tr>
                                    <tr><td><input type="submit" value="Add"></td></tr>
                                </table>
                            </form></td></tr>
                    </jstlc:if>
                </table>
            </jstlc:when>

            <jstlc:when test="<%= data.getResourceRequestType() == ResourceRequestType.MULTIPLE_RESOURCES %>">
                <table id="usersTable" class="resourcesTable">
                    <tr><td>User</td><td>Account Status</td><td></td><td></td></tr>
                <jstlc:forEach var="user" items="${data.resourcesList}">
                    <tr><td>${user.userData.username}</td><td>${user.accountLevel}</td>
                        <td><a class="operationsLink" href="users.jsp?id=${user.userData.userId}">View</a></td>
                        <td><a class="operationsLink" href="manage_user.jsp?action=3&amp;id=${user.userData.userId}"
                               onclick="return resourceRemovalVerification('${user.userData.username}', this);">Remove</a></td></tr>
                </jstlc:forEach>
                </table>
                <script type="text/javascript">
                    var usersPagination = new ElementsTablePagination('usersTable', 'usersPagination');
                </script>
                Page Size: <input type="text" size="2" id="itemsPerPage" value="20">
                <a class="pageSizeUpdateLink" href="noJavaScript.jsp"
                   onclick="return usersPagination.setMaxNumberOfItemsPerPage(document.getElementById('itemsPerPage').value);">Update</a>
                <div class="creationOperations">
                <a class="creationLink" href="noJavaScript.jsp" onclick="return changePopupVisibility('formCreateUser');">Add User</a>
                <form class="resourceCreationElement" id="formCreateUser" action="manage_user.jsp?action=1" method="post">
                    <table>
                        <tr><td>Create User:</td></tr>
                        <tr><td>Username:</td><td><input type="text" name="user.create.name"></td></tr>
                        <tr><td>User Password:</td><td><input type="password" name="user.create.password"></td></tr>
                        <tr><td>Confirm Password:</td><td><input type="password" name="user.create.password.confirm"></td></tr>
                        <tr><td>Account Level:</td><td><select name="user.create.level">
                                    <option value="0">Disabled</option><option value="1">Normal</option>
                                    <option value="2">Manager</option><option value="3">Owner</option></select></td></tr>
                        <tr><td><input type="submit" value="Create"></td></tr>
                    </table>
                </form>
                </div>
            </jstlc:when>

            <jstlc:otherwise>
                Invalid resource request type: ${data.resourceRequestType}.
            </jstlc:otherwise>
        </jstlc:choose>
    </jstlc:when>

    <jstlc:otherwise>
        Error: ${data.exception.exceptionMessage}
    </jstlc:otherwise>
</jstlc:choose>
<%-- End of Page Body Content --%>